package com.ss.android.lockscreen.d.a;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.ss.android.lockscreen.activity.lock.i;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class c {
    public interface a {
        void a(List arg1);
    }

    final class b extends SQLiteOpenHelper {
        b(c arg4, Context arg5) {
            super(arg5, arg4.a, null, 1);
        }

        public final void onCreate(SQLiteDatabase arg1) {
        }

        public final void onUpgrade(SQLiteDatabase arg1, int arg2, int arg3) {
        }
    }

    public static final class c$c {
        public final i b;
        public final List a;

        public c$c(i arg1, List arg2) {
            this.b = arg1;
            this.a = arg2;

        }
    }

    String a;
    Handler b;
    private static c c;
    private b d;
    private Handler e;

    private c() {
        this.a = "LockScreen.db";
        HandlerThread v0 = new HandlerThread("DataBaseManagerThread");
        v0.start();
        this.e = new Handler(v0.getLooper());
        this.b = new Handler(Looper.getMainLooper());
    }

    public synchronized final void a(Context arg3, com.ss.android.lockscreen.d.a.b arg4, a arg5) {
            this.e.post(new f(this, arg3, arg4, arg5));
    }

    public static c a() {
        if(c.c == null) {
            Class v1 = c.class;
            synchronized (v1) {
                if (c.c == null) {
                    c.c = new c();
                }
            }
        }

        return c.c;
    }

    public synchronized final void a(Context arg3, com.ss.android.lockscreen.d.a.b arg4, c$c arg5) {
            this.e.post(new h(this, arg3, arg4, arg5));
    }

    public synchronized final List a(Context arg11, com.ss.android.lockscreen.d.a.b arg12) {
        com.ss.android.lockscreen.d.a.aa v3;
        ArrayList v1_1;
        Cursor v2 = null;
        SQLiteDatabase v0_2 = null;
        List v0;
        List v9 = null;
        if(arg12 == null) {
            v0 = v9;
            return v0;
        }

        try {
            if(this.d == null) {
                this.d = new b(this, arg11.getApplicationContext());
            }

            v0_2 = this.b();
            if(v0_2 != null) {
            }else{
                v0 = v9;
            }
            v0_2.beginTransaction();
            arg12.a(v0_2);
            com.ss.android.lockscreen.d.a.a.b v7 = new com.ss.android.lockscreen.d.a.a.b();
            arg12.a(v7);
            v2 = v0_2.query(arg12.a, null, v7.a, v7.b, null, null, v7.c, null);
            v1_1 = new ArrayList();
            v2.moveToFirst();
            while(true) {
                if(v2.isAfterLast()) {
                    break;
                }

                if(arg12.b) {
                    v3 = arg12.a(v2);
                }
                else {
                    v3 = ((com.ss.android.lockscreen.d.a.aa)v9);
                }

                if(v3 != null) {
                    ((List)v1_1).add(v3);
                }
                v2.moveToNext();
                continue;
            }
            v0_2.setTransactionSuccessful();
            if(v2 != null) {
                try {
                    v2.close();
                }catch (Exception e){

                }
            }

            v0_2.endTransaction();
           return v1_1;
        } catch(Throwable v1) {
            v1.printStackTrace();
            if(v2 != null) {
                try {
                    v2.close();
                }catch (Exception e){

                }
            }
            v0_2.endTransaction();
            return v9;
        }
    }

    public synchronized final void a(Context arg6, com.ss.android.lockscreen.d.a.b arg7, List arg8) {
        SQLiteDatabase v1 = null;
        try {
            if(this.d == null) {
                this.d = new b(this, arg6.getApplicationContext());
            }

            v1 = this.b();
            if(v1 != null) {
                v1.beginTransaction();
                arg7.a(v1);
                Iterator v2 = arg8.iterator();
                while(v2.hasNext()) {
                    Object v0_1 = v2.next();
                    ContentValues v3 = new ContentValues();
                    arg7.a(v3, ((com.ss.android.lockscreen.d.a.aa)v0_1));
                    v1.insert(arg7.a, null, v3);
                }
                v1.setTransactionSuccessful();
            }
        } catch(Throwable v0) {
        }finally {
            v1.endTransaction();
        }
    }

    public synchronized final void b(Context arg3, com.ss.android.lockscreen.d.a.b arg4, List arg5) {
            this.e.post(new d(this, arg3, arg4, arg5));
    }

    private SQLiteDatabase b() {
        SQLiteDatabase v0 = null;
        if(this.d != null) {
            try {
                v0 = this.d.getWritableDatabase();
            }
            catch(Throwable v1) {
                com.ss.android.lockscreen.utils.a.a(v1);
            }
        }

        return v0;
    }

    public synchronized final void b(Context arg5, com.ss.android.lockscreen.d.a.b arg6) {
        SQLiteDatabase v1 = null;
        if (arg6 != null) {
            try {
                if (this.d == null) {
                    this.d = new b(this, arg5.getApplicationContext());
                }

                v1 = this.b();
                if (v1 == null) {
                    v1.beginTransaction();
                    arg6.a(v1);
                    com.ss.android.lockscreen.d.a.a.a v0_1 = new com.ss.android.lockscreen.d.a.a.a();
                    arg6.a(v0_1);
                    v1.delete(arg6.a, v0_1.a, v0_1.b);
                    v1.setTransactionSuccessful();
                }
            } catch (Throwable v0) {
                v0.printStackTrace();
            } finally {
                v1.endTransaction();
            }
        }
    }
}

